package com.example.hms.controller;

import com.example.hms.entity.User;
import com.example.hms.service.UserService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

//使用@RestController可以代替@Controller和@ResponseBady
@RestController
@RequestMapping("/user")
public class UserController {
    //动态注入下一层UserService（服务层）
    @Resource
    private UserService userService;

    //声明方法 声明请求方式   @RequestMapping标识具体路径
    //Result 返回数据的统一包装类
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody Map<String,String> map, HttpServletRequest request) {
        String userId = map.get("userId");
        String password = map.get("userPassword");
        if (userId == null || password == null || userId.equals("")  || password.isEmpty()) {
            return ResultSubmit.error("输入参数不全");
        }
        return userService.login(userId,password,request);
    }

    @RequestMapping(value = "/login2",method = RequestMethod.POST)
    public Result login2(String userId,String userPassword, HttpServletRequest request) {
        return userService.login(userId,userPassword,request);
    }

    //GET方法不需要封装参数
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public Result info(HttpServletRequest request) {
        return userService.info(request);
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public Result logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("userId",null);
        session.setAttribute("token",null);
        return ResultSubmit.success("退出成功");
    }

    @RequestMapping(value = "/selectAllUser",method = RequestMethod.GET)
    public Result selectAllUser(String userId, String userName, String roleName) {
        return userService.selectAllUser(userId,userName,roleName);
    }

    @RequestMapping(value = "/insert",method = RequestMethod.GET )
    public Result insert(User user, HttpServletRequest request) {
        return userService.insert(user,request);
    }

    @RequestMapping(value = "/update",method = RequestMethod.GET)
    public Result update(User user, HttpServletRequest request) {return userService.update(user,request);}

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(String userId, HttpServletRequest request) {return userService.delete(userId,request);}

}
